package application.filiale;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JSeparator;
import javax.swing.JButton;

import mware_lib.*;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JTextArea;

import branch_access.*;

/**
 * Filiale:
 * contains the GUI for the Filiale and allows the FilialManager to create and delete Accounts.
 * @author Rico Flaegel
 * @author Jan Quenzel
 */
public class Filiale extends JFrame {
	private static final long serialVersionUID = 5215633335012231591L;
	private JPanel contentPane;
	private JTextField txtarea_hostname;
	private JTextField textField_Port;
	private JTextField txt_managername;
	private JTextField textField_CustomerName;
	/**
	 * @uml.property  name="ns"
	 * @uml.associationEnd  
	 */
	private NameService ns;
	private JButton btnMitBankVerbinden;
	/**
	 * @uml.property  name="manager"
	 * @uml.associationEnd  
	 */
	private Manager manager;
	private JTextArea textArea;
	private JLabel lblKontonummer;
	private JTextField textField_accID;
	private JButton btnKontoLschen;
	/**
	 * Launch the application.
	 */
	public static void main(final String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				String hostName = null;
				String bankname = null;
				int bankPort = 0;
				try {
					//valid args
					if(args.length == 3){
						hostName = args[0];
						bankname = args[2];
						try{
							bankPort = Integer.parseInt(args[1]);
						}catch(NumberFormatException e){
							System.out.println("port is not valid");
							return;
						}//start filiale
						Filiale frame = new Filiale(hostName, bankname, bankPort);
						frame.setVisible(true);
						//invalid args
					}else if(args.length != 3 ){
						System.out.println("usage: Filiale_Test HOSTNAME PORT BANKNAME");
					}
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public Filiale(String hostName, String bankname, int bankPort){
		this();
		txtarea_hostname.setText(hostName);
		txtarea_hostname.setEditable(false);
		txt_managername.setText(bankname);
		txt_managername.setEditable(false);
		textField_Port.setText(bankPort+"");
		textField_Port.setEditable(false);
	}
	
	public Filiale() {
		setTitle("Filial-Applikation");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 668, 498);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		txtarea_hostname = new JTextField();
		txtarea_hostname.setText("localhost");
		txtarea_hostname.setBounds(127, 11, 86, 20);
		contentPane.add(txtarea_hostname);
		txtarea_hostname.setColumns(10);
		
		JLabel lblHostnameDerBank = new JLabel("Hostname der Bank");
		lblHostnameDerBank.setBounds(10, 14, 126, 14);
		contentPane.add(lblHostnameDerBank);
		
		textField_Port = new JTextField();
		textField_Port.setText("2000");
		textField_Port.setBounds(263, 11, 86, 20);
		contentPane.add(textField_Port);
		textField_Port.setColumns(10);
		
		JLabel lblPort = new JLabel("Port");
		lblPort.setBounds(223, 14, 46, 14);
		contentPane.add(lblPort);
		
		JSeparator separator = new JSeparator();
		separator.setBounds(10, 42, 632, 14);
		contentPane.add(separator);
		
		txt_managername = new JTextField();
		txt_managername.setText("Boss");
		txt_managername.setBounds(117, 67, 86, 20);
		contentPane.add(txt_managername);
		txt_managername.setColumns(10);
		
		JLabel lblNameDerBank = new JLabel("Name der Bank");
		lblNameDerBank.setBounds(10, 67, 96, 14);
		contentPane.add(lblNameDerBank);
		
		textField_CustomerName = new JTextField();
		textField_CustomerName.setEditable(false);
		textField_CustomerName.setBounds(117, 198, 86, 20);
		contentPane.add(textField_CustomerName);
		textField_CustomerName.setColumns(10);
		
		JLabel lblNeuesKontoAnlegen = new JLabel("Neues Konto anlegen:");
		lblNeuesKontoAnlegen.setBounds(10, 176, 126, 14);
		contentPane.add(lblNeuesKontoAnlegen);
		
		JLabel lblKundenname = new JLabel("Kundenname");
		lblKundenname.setBounds(10, 201, 86, 14);
		contentPane.add(lblKundenname);
		
		btnMitBankVerbinden = new JButton("Konto erstellen");
		btnMitBankVerbinden.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				String accID = manager.createAccount(textField_CustomerName.getText());
				addID(accID);
			}
		});
		btnMitBankVerbinden.setEnabled(false);
		btnMitBankVerbinden.setBounds(223, 197, 126, 23);
		contentPane.add(btnMitBankVerbinden);
		
		JButton btnMitBankVerbinden = new JButton("Mit Bank Verbinden");
		btnMitBankVerbinden.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				establishConnection();
			}
		});
		btnMitBankVerbinden.setBounds(221, 67, 155, 23);
		contentPane.add(btnMitBankVerbinden);
		
		textArea = new JTextArea();
		textArea.setBounds(10, 258, 632, 191);
		contentPane.add(textArea);
		
		lblKontonummer = new JLabel("Kontonummer");
		lblKontonummer.setBounds(10, 226, 86, 14);
		contentPane.add(lblKontonummer);
		
		textField_accID = new JTextField();
		textField_accID.setEditable(false);
		textField_accID.setBounds(117, 229, 86, 20);
		contentPane.add(textField_accID);
		textField_accID.setColumns(10);
		
		btnKontoLschen = new JButton("Konto l\u00F6schen");
		btnKontoLschen.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				deleteKonto();
			}
		});
		btnKontoLschen.setEnabled(false);
		btnKontoLschen.setBounds(223, 224, 126, 23);
		contentPane.add(btnKontoLschen);
	}
	/**
	 * activates the fields for entering bank name and port 
	 */
	public void setComponentsActive(){
		btnMitBankVerbinden.setEnabled(true);
		textField_CustomerName.setEditable(true);
		btnKontoLschen.setEnabled(true);
		textField_accID.setEditable(true);
	}
	
	/**
	 * add text to log part of the GUI
	 * @param text
	 */
	public void addID(String text){
		if(text != null){
			textArea.append(textField_CustomerName.getText() + " hat die Kontonummer " + text + " bekommen" + "\n");
		}else{
			textArea.append("Kontoerstellung fehlgeschlagen"+"\n");
		}
	}
	
	/**
	 * delete the Account defined by the account id
	 */
	private void deleteKonto(){
		boolean b = manager.removeAccount(textField_accID.getText());
		if(b){
			textArea.append("das Konto " + textField_accID.getText()  +" wurde erfolgreich geloescht\n");
		}else{
			textArea.append("das Konto " + textField_accID.getText()  +" konnte nicht geloescht werden\n");
		}
	}

	/**
	 * connect the Filiale Manager to the BankManager and get ManagerStub
	 */
	private void establishConnection(){
		String hostname = txtarea_hostname.getText();
		String port = textField_Port.getText();
		String managername = txt_managername.getText();
		int port_i = 0;
		try{
			port_i = Integer.parseInt(port);
		}catch(NumberFormatException ne){
			JOptionPane.showMessageDialog(null,"der angegebene Port ist keine Ganzzahl!","Fehler", JOptionPane.ERROR_MESSAGE);
			return;
		}
		
		ObjectBroker brk = ObjectBroker.getBroker(hostname, port_i);
		ns = brk.getNameService();
		manager = (Manager) ns.resolve(managername); // get Mananger
		if(manager == null){
			JOptionPane.showMessageDialog(null,"Es existiert keine Bank/Manager mit diesem Namen oder es gibt Probleme mit dem Namensserver!","Fehler", JOptionPane.ERROR_MESSAGE);
		}else{
			setComponentsActive();
		}
	}
}
